package com.yanqu.road.server.channelserver.timer.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ChannelTonBingProto;
import com.yanqu.road.server.channelserver.logic.ChannelMessageHelper;
import com.yanqu.road.server.channelserver.manager.activity.ChannelActivityInfoMgr;
import com.yanqu.road.server.channelserver.manager.activity.ChannelActivityUnionMgr;
import com.yanqu.road.server.channelserver.manager.activity.tonbing.ChannelTonBingMgr;
import com.yanqu.road.server.channelserver.manager.activity.tonbing.activity.ChannelTonBingActivity;
import com.yanqu.road.server.channelserver.manager.server.ChannelRegisterServerMgr;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;
import java.util.Map;

//有需要商会信息的去商会跨服拉
public class ChannelXUnionTask extends GeneralTask {

    @Override
    public void doRun() {
        List<Integer> registerChannelList = ChannelRegisterServerMgr.getChannelIdList();
        for(int channelId : registerChannelList){
            if(ChannelActivityUnionMgr.isUpdateChanelServer(channelId)){
                ChannelTonBingProto.ChannelGetUnionReqMsg.Builder builder = ChannelTonBingProto.ChannelGetUnionReqMsg.newBuilder();
                builder.setActivityId(0);
                builder.setTopCount(0);
                builder.setChannelId(channelId);
                //去每个服拉渠道对应的区服信息囧
                YanQuMessage message = YanQuMessageUtils.buildMessage(Cross3Protocol.C3_ACTIVITY_GET_UNION_INFO, builder);
                ChannelMessageHelper.sendToChannel(message, channelId);
            }
        }


        Map<Integer, ActivityInfo> activityInfoMap = ChannelActivityInfoMgr.getActivityInfoMap();
        for(ActivityInfo acInfo : activityInfoMap.values()){
            if(acInfo.getType() == eActivityType.ChannelTonBingActivity.getValue()){
                ChannelTonBingActivity ac = ChannelTonBingMgr.getActivity(acInfo.getActivityId());
                if(ac != null && ac.getConfig() != null){
                    long overTime = ac.getConfig().getJoinTimeList().get(1) + DateHelper.MINUTE_MILLIONS;
                    if(ChannelActivityUnionMgr.containsActivity(acInfo) && System.currentTimeMillis() > overTime && ac.getChannelTonBingDataMain().isLockUnion()){
                        continue;
                    }
                }
                List<Integer> channelIdList = StringUtils.stringToIntegerList(acInfo.getParam6(),";");
                if(channelIdList.size() != 3){
                    getLOGGER().error("活动{}，跨域统兵必须是三个渠道，不是三个渠道不给开启活动，请过期重新导入三个渠道",acInfo.getActivityId());
                    continue;
                }
                if(ac != null && ac.getConfig() != null){
                    for(int channelId : channelIdList){
                        ChannelTonBingProto.ChannelGetUnionReqMsg.Builder builder = ChannelTonBingProto.ChannelGetUnionReqMsg.newBuilder();
                        builder.setActivityId(acInfo.getActivityId());
                        builder.setChannelId(channelId);
                        builder.setTopCount(ac.getConfig().getUnionChannelNum() + 50);
                        builder.addAllServerId(acInfo.getServerIdList());
                        YanQuMessage message = YanQuMessageUtils.buildMessage(Cross3Protocol.C3_ACTIVITY_GET_UNION_INFO, builder);
                        ChannelMessageHelper.sendToChannel(message, channelId);
                        getLOGGER().debug("活动{}，发送跨域统兵请求商会消息，渠道{}",acInfo.getActivityId(),channelId);
                    }
                }
            }
        }
    }
}
